package org.libl.dao.car.impl;

import org.libl.utils.JDBCPoolUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.libl.dao.car.CarDao;
import org.libl.domain.Car;

import java.util.List;

public class CarDaoImpl implements CarDao {
    //1.创建模板类对象
    private JdbcTemplate jt = new JdbcTemplate(JDBCPoolUtils.getDs());
    public List<Car> selectAll(){
        List<Car> list = jt.query(
                "select * from tab_car",
                new BeanPropertyRowMapper<Car>(Car.class));
        return list;
    }

    public int addCar(String car_sn,String car_name,String car_stutus,String car_jiage){
        //1.编写sql
        String sql = "insert into tab_car(car_sn,car_name,car_stutus,car_jiage) values(?,?,?,?)";
        //2.调用更新方法
        int row = jt.update(sql,car_sn,car_name,car_stutus,car_jiage);

        return row;
    }

    public int del(String car_id){

        //1.编写sql
        String sql = "delete from tab_car where car_id = ?";
        //2.调用更新方法
        int row = jt.update(sql,Integer.parseInt(car_id));

        return row;
    }

    public int edit(String car_id, String car_sn,String car_name,String car_stutus,String car_jiage){

        //1.编写sql
        String sql = "update tab_car set car_sn=?,car_name=?,car_stutus=?,car_jiage=? where car_id = ?";
        //2.调用更新方法
        int row = jt.update(sql,car_sn,car_name,car_stutus,car_jiage,Integer.parseInt(car_id));
        return row;
    }
}
